package com.www.applyword.main;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

import com.tencent.imsdk.TIMCallBack;
import com.tencent.imsdk.TIMManager;
import com.tencent.qcloud.tim.uikit.TUIKit;
import com.tencent.qcloud.tim.uikit.base.IUIKitCallBack;
import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.utils.SPUtil;

public class SplashActivity extends Activity {

    private static final String TAG = SplashActivity.class.getSimpleName();
    private static final int SPLASH_TIME = 1500;
    private View mFlashView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        mFlashView = findViewById(R.id.flash_view);
        handleData();
    }

    private void handleData() {
        mFlashView.postDelayed(new Runnable() {
            @Override
            public void run() {
                startLogin();
            }
        }, SPLASH_TIME);
    }

    private void startLogin() {
        if (SPUtil.getBoolean("login")){
            SPUtil.put("login", true);
            startActivity(new Intent(SplashActivity.this, MainActivity.class));
            finish();
           /* TIMManager.getInstance().autoLogin(SPUtil.getString(Constants.PHONE), new TIMCallBack() {
                @Override
                public void onError(int errCode, String errMsg) {
                    //  Utils.showToastSafe("login failed. code: " + errCode + " -------errmsg: " + errMsg);
                        TUIKit.login(SPUtil.getString(Constants.PHONE),SPUtil.getString(Constants.USERSIG), new IUIKitCallBack() {
                            @Override
                            public void onSuccess(Object data) {
                                SPUtil.put("login", true);
                                startActivity(new Intent(SplashActivity.this, MainActivity.class));
                                finish();
                            }

                            @Override
                            public void onError(String module, int errCode, String errMsg) {
                                //错误码 code 和错误描述 desc，可用于定位请求失败原因
                                //错误码 code 列表请参见错误码表
                                Log.d(Constants.TAG, "login failed. code: " + errCode + " errmsg: " + errMsg);
                             //  Utils.showToastSafe("login failed. code: " + errCode + " -------errmsg: " + errMsg);
                                TUIKit.unInit();
                                if (errCode == 7501){
                                    startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                                    finish();
                                }
                            }
                        });
                }

                @Override
                public void onSuccess() {
                    startActivity(new Intent(SplashActivity.this, MainActivity.class));
                    finish();
                }
            });*/

        }else {
            Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }
}
